package com.walker.rest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.walker.service.meta.AssetMetaDataCollectServ;
import com.walker.service.meta.SyncMetaHiveService;
import com.walker.util.CommonUtils;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/rest/syncRest")
public class SyncRestController {

	private static final Log log = LogFactory.getLog(SyncRestController.class);
	
	@Resource 
	private SyncMetaHiveService syncMetaHiveService;
	
	@Resource
	private AssetMetaDataCollectServ syncMetaService;
	
	/**
	 * hive元数据同步
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@GetMapping("syncHiveMeta")
	public String syncHiveMeta(HttpServletRequest request) throws Exception {
		JSONObject json = new JSONObject();
		try {
			String project_code = CommonUtils.valueOf(request.getParameter("project_code"));
			if(StringUtils.isNotBlank(project_code)){
				syncMetaHiveService.syncHiveMeta(project_code);
			}else{
				syncMetaHiveService.syncHiveMeta();
			}
			json.put("msg","元数据同步完成");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
			json.put("msg",e.getMessage());
		}
		return json.toString();
	}
	
	@GetMapping("syncMeta")
	public String syncMeta(HttpServletRequest request) throws Exception {
		String project_code = CommonUtils.valueOf(request.getParameter("project_code"));
		JSONObject json = new JSONObject();
		try {
			syncMetaService.syncMeta(project_code);
			json.put("msg","元数据同步完成");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
			json.put("msg",e.getMessage());
		}
		return json.toString();
	}
}
